package com.rpcservice.rpc.loadBalance;

import com.rpcservice.rpc.common.utils.CollectionUtil;
import com.rpcservice.rpc.remoting.dto.RpcRequest;

import java.util.List;

/**
 * Abstract class for a load balancing policy
 */
public abstract class AbstractLoadBalance implements LoadBalance {

    @Override
    public String selectServiceAddress(List<String> serviceUrlList, RpcRequest rpcRequest) {
        //1.判断服务地址列表是否为空
        if (CollectionUtil.isEmpty(serviceUrlList)) {
            return null;
        }
        //2.如果服务地址列表中只有一个地址，直接返回该地址
        if (serviceUrlList.size() == 1) {
            return serviceUrlList.get(0);
        }
        //3.如果服务地址列表中有多个地址，使用钩子函数进行选择
        return doSelect(serviceUrlList, rpcRequest);
    }

    protected abstract String doSelect(List<String> serviceUrlList, RpcRequest rpcRequest);
}
